package com.quick.develop.flowable.framework.core;

import cn.hutool.core.util.StrUtil;
import com.quick.develop.flowable.application.IBpmTaskAssigneeApplication;
import com.quick.develop.flowable.constant.BpmConstants;
import com.quick.develop.framework.utils.StringUtils;
import com.quick.develop.framework.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEvent;
import org.flowable.common.engine.api.delegate.event.FlowableEventListener;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.event.impl.FlowableProcessStartedEventImpl;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @author junyuan.liu
 * @description: 流程启动监听 挂载全局
 * 设置流程名称 全局变量 主要考虑子流程
 * @date 2022/5/30 16:19
 */
@Component
@Slf4j
public class BpmProcessStartedListener implements FlowableEventListener {
    @Resource
    protected RuntimeService runtimeService;

    @Resource
    protected IBpmTaskAssigneeApplication bpmTaskAssigneeApplication;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void onEvent(FlowableEvent flowableEvent) {
        ExecutionEntityImpl entity = (ExecutionEntityImpl) ((FlowableProcessStartedEventImpl) flowableEvent).getEntity();
        ExecutionEntityImpl parent = entity.getParent();
        ExecutionEntityImpl superExecution = parent.getSuperExecution();
        String userCode = StringUtils.isEmpty(parent.getStartUserId()) ? SecurityUtils.getUserCode() : parent.getStartUserId();
        String userName = bpmTaskAssigneeApplication.getUserNameByUserCode(userCode);
        //DelegateExecution execution = ((FlowableProcessStartedEventImpl) flowableEvent).getExecution();
        String processInstanceId = entity.getProcessInstanceId();
        String businessKey = parent.getBusinessKey();
        String title = this.setFormProperty(entity);
        String processDefinitionName = parent.getProcessDefinitionName();
        // 子流程启动者
        if (Objects.nonNull(superExecution)) {
            // String subStartUser = bpmExecutionService.getSubStartUser(superExecution.getParentId());
            userCode = parent.getStartUserId();
            userName = bpmTaskAssigneeApplication.getUserNameByUserCode(userCode);
        }
        title = StrUtil.isBlank(title) ? processDefinitionName : title;
        if (StrUtil.isEmpty(parent.getName())) {
            runtimeService.setProcessInstanceName(processInstanceId, title);
            parent.setVariable(BpmConstants.PROCESS_NAME, title);
        }
        if (Objects.isNull(parent.getVariable(BpmConstants.INITIATOR))) {
            parent.setVariable(BpmConstants.INITIATOR, userCode);
        }
        if (Objects.isNull(parent.getVariable(BpmConstants.INITIATOR_NAME))) {
            parent.setVariable(BpmConstants.INITIATOR_NAME, userName);
        }
    }

    @Override
    public boolean isFailOnException() {
        return true;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }

    public String setFormProperty(ExecutionEntityImpl entity) {
        // TODO 设置标题等
        return null;
    }
}
